package com.weita.lightfood.controller;


import com.weita.lightfood.model.entity.User;
import com.weita.lightfood.model.query.user.RegisterUserQuery;
import com.weita.lightfood.model.query.user.ResetPasswordQuery;
import com.weita.lightfood.model.vo.user.UserHistoryWeight;
import com.weita.lightfood.model.vo.user.UserWeightVo;
import com.weita.lightfood.service.UserService;
import com.weita.lightfood.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author IT_Monster at 2019/03/27 15:01.
 *
 * 用户管理接口
 */
@Api(tags = "用户管理接口")
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 通过id获取单个用户信息
     * @param id
     * @return
     */
    @ApiOperation("通过id获取单个用户信息")
    @ApiImplicitParam(name = "userId",value = "用户id",defaultValue = "44")
    @PostMapping("/getByID")
    public Result<User> getUserByID(@RequestParam("userId") int userId) {
        try {
            return Result.success(userService.getByID(userId));
        } catch (Exception e) {
            return Result.fail("获取用户信息出错");
        }
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @ApiOperation("获取所有用户信息")
    @PostMapping("/getAllUsers")
    public Result<List<User>> getAllUsers(){
        try {
            return Result.success(userService.getAll());
        } catch (Exception e) {
            return Result.fail("获取所有用户信息出错");
        }
    }

    /**
     * 新用户注册设置密码和密保问题
     * @param registerUserQuery
     * @return
     */
    @ApiOperation("注册用户接口")
    @PostMapping("/registerUser")
    public Result<String> registerUser(RegisterUserQuery registerUserQuery) {
        try {
            return Result.success(userService.registerUser(registerUserQuery));
        } catch (Exception e) {
            System.out.println("注册用户出错--------------------->"+registerUserQuery);
            return Result.fail("注册用户出错");
        }
    }

    /**
     * 更新用户信息 -------------------->  接口似乎有问题 2019.05.04（有的值传不进数据库）
     * @param user
     * @return
     */
    @ApiOperation("更新用户信息接口")
    @PostMapping("/update")
    public Result<String> update(User user) {
        try {
            return Result.success(userService.update(user));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("更新用户信息出错-------------------------------->" + user);
            return Result.fail("更新用户信息出错");
        }
    }

    /**
     * 通过id删除用户
     * @param userId
     * @return
     */
    @ApiOperation("通过id删除用户接口")
    @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "44")
    @PostMapping("/delete")
    public Result<String> delete(int userId) {
        try {
            return Result.success(userService.delete(userId));
        } catch (Exception e) {
            System.out.println( "通过id删除用户接口---------------------->" + userId);
            return Result.fail("删除用户出错");
        }
    }


    /**
     * 修改用户密码
     * @param userId
     * @param newPwd
     * @return
     */
    @ApiOperation("修改用户密码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPwd", value = "旧密码"),
            @ApiImplicitParam(name = "newPwd", value = "新密码"),
            @ApiImplicitParam(name = "confirmPwd", value = "确认密码"),
            @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0")
    })
    @PostMapping("/updatePwd")
    public Result<String> updatePwd(int userId, String oldPwd, String newPwd, String confirmPwd) {
        try {
            return Result.success(userService.updatePwd(userId, oldPwd, newPwd, confirmPwd));
        } catch (Exception e) {
            System.out.println("----------------------->修改用户密码出错");
            return Result.fail("修改用户密码出错");
        }
    }

    /**
     * 获取用户密保问题
     * @param userName
     * @return
     */
    @ApiOperation("获取用户密保问题接口")
    @ApiImplicitParam(name = "userName", value = "用户名")
    @PostMapping("/getSecurityQuestion")
    public Result<String> getSecurityQuestion(String userName) {
        try {
            return Result.success(userService.getSecurityQuestion(userName));
        } catch (Exception e) {
            System.out.println("获取用户密保问题接口出错");
            return Result.fail("获取用户密保问题出错");
        }
    }

    /**
     * 重置用户密码
     * @param resetPasswordQuery
     * @return
     */
    @ApiOperation("重置用户密码接口")
    @PostMapping("/resetPassword")
    public Result<String> resetPassword(ResetPasswordQuery resetPasswordQuery) {
        try {
            return Result.success(userService.resetPassword(resetPasswordQuery));
        } catch (Exception e) {
            System.out.println("重置用户密码接口出错");
            return Result.fail("重置用户密码出错");
        }
    }

    /**
     * 获取用户初始体重和目标体重以及最新体重
     */
    @ApiOperation("获取用户初始体重和目标体重以及最新体重接口")
    @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0")
    @PostMapping("/getWeight")
    public Result<UserWeightVo> getWeight(int userId){
        try {
            return Result.success(userService.getWeight(userId));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取用户初始体重和目标体重以及最新体重接口出错");
            return Result.fail("获取用户初始体重和目标体重以及最新体重出错");
        }
    }

    /**
     * 记录用户最新体重
     * @param userId
     * @param newWeight
     * @return
     */
    @ApiOperation("记录用户最新体重接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0"),
            @ApiImplicitParam(name = "newWeight", value = "最新体重", defaultValue = "0"),
            @ApiImplicitParam(name = "date", value = "记录日期", defaultValue = "0")
    })
    @PostMapping("/recordWeight")
    public Result<String> recordWeight(int userId, double newWeight, String date){

        try {
            return Result.success(userService.recordWeight(userId, newWeight, date));
        } catch (Exception e) {
            System.out.println("记录用户最新体重接口出错");
            return Result.fail("记录用户最新体重出错");
        }
    }

    /**
     * 获取用户历史体重
     * @param userId
     * @return
     */
    @ApiOperation("获取用户历史体重")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0"),
    })
    @PostMapping("/getUserHistoryWeight")
    public Result<List<UserHistoryWeight>> getUserHistoryWeight(int userId){
        try {
            return Result.success(userService.getUserHistoryWeight(userId));
        } catch ( Exception e ) {
            e.printStackTrace();
            return Result.fail("获取用户历史体重出错");
        }
    }

}
